// pages/question/content.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    chuantu: [],
    xyz: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.doctor();
  },
  titleinput(e) {
    this.setData({
      "title": e.detail.value,
    })
  },
  contentinput(e) {
    this.setData({
      "content": e.detail.value,
    })
  },
  // 医生列表
  doctor() {
    var _this = this;
    var access_token = app.globalData.access_token;
    var params = {
      url: app.api_list.get_doctor_list,
      method: 'get',
      header: {
        "Access-Token": access_token,
        'Content-Type': 'application/json'
      },
      data: {
        Page: 1,
        limit: 15,
      },
      callBack: function (response) {
        console.log(response);
        if (response.data.data.length) {
          console.log(response.data.data);
          _this.setData({
            doctor: response.data.data,
          });
          console.log(_this.data.doctor);
        }
      },
      fail: function (error) {
        console.error('请求服务器失败：', error);
      }
    };
    app.http.request(params)
  },
  // 添加图
  chooseImage: function () {
    var _this = this;
    // var access_token = app.globalData.access_token;
    var access_token = wx.getStorageSync("token");
    wx.chooseImage({
      success: function (res) {
        console.log(res);
        const tempFilePaths = res.tempFilePaths;
        var chuantu = _this.data.chuantu;
        for (var i = 0; i < tempFilePaths.length; i++) {
          chuantu.push(tempFilePaths[i]);
        }
        _this.setData({
          'chuantu': chuantu,
        })
        wx.uploadFile({
          url: app.config.domain + app.api_list.upload,
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            "Access-Token": access_token,
          },
          success: function (res) {
            console.log(JSON.parse(res.data));
            var t = JSON.parse(res.data);
            console.log(t.name);
            if(t){
              _this.setData({
                "cover_image":t.data.name,
              })
              console.log(_this.data.cover_image);
            }
            // JSON.parse(res.data);
          },
          fail: function (res) {
            console.log('fail', res);
          },
          complete: function (res) {
            console.log('complete', res);
          }
        })
      }
    })
  },
  // 删除图
  shanchu(e) {
    var index = e.currentTarget.dataset.index;
    var chuantu = this.data.chuantu;
    var _this = this;
    var access_token = app.globalData.access_token;
    var params = {
      url: app.api_list.delete,
      method: 'POST',
      header: {
        "Access-Token": access_token,
        'Content-Type': 'application/json'
      },
      data: {
        file_url: _this.data.cover_image,
      },
      callBack: function (response) {
        console.log(response);
        chuantu.splice(index, 1);
        _this.setData({
          chuantu: chuantu,
        });
      },
      fail: function (error) {
        console.error('请求服务器失败：', error);
      }
    };
    app.http.request(params);

  },
  // 选择
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      xyz: e.detail.value,
    });
  },
  sendmessage() {
    var title = this.data.title;
    var content = this.data.content;
    var doctor_id = this.data.doctor[this.data.xyz].id;
    console.log(title, content, doctor_id);
    if (!title) {
      wx.showToast({
        title: '请填写标题',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!content || content.length < 15) {
      wx.showToast({
        title: '内容不能小于15字',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!doctor_id) {
      wx.showToast({
        title: '请选择医生',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    var params = {
      url: app.api_list.sendmessage,
      method: 'post',
      data: {
        title: title,
        content: content,
        doctor_id: doctor_id,
        image: this.data.cover_image,
        message_id: 0,
      },
      callBack: function (response) {
        console.log(response);
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: "留言成功",
          success: function (res) {
            wx.navigateBack({
              delta: 1
            });
          }
        })
      },
      fail: function (error) {
        console.error('请求服务器失败：', error);
      }
    };
    app.http.request(params);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})